package com.oracle.zebrafreelytraveling.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

import com.oracle.zebrafreelytraveling.App;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.data.model.NaviInfoModel;
import com.oracle.zebrafreelytraveling.net.HttpManager;

import java.util.List;

/**
 * Created by yydcnjjw on 2017/8/9.
 */

public class UserUtil {

    public static final String USER_NET = "userNet";
    public static final String USER_LOCAL = "userLocal";
    public static final String NAVI_MODEL_LIST = "NaviInfoModelList";
    private static Context mContext = App.getContextObject();

    public static boolean isLogin() {
        User user = getUserInfoLocalBySP();
        if (user != null) {
            return true;
        } else {
            ToastUtil.showToast(mContext.getString(R.string.no_login));
            return false;
        }

    }

    public static NaviInfoModel getUserNaviModelList(){
        String naviInfoJson = SharePreUtil.GetShareString(NAVI_MODEL_LIST);
        if (naviInfoJson.length() <= 0){
            return null;
        } else {
            NaviInfoModel naviModelList = GsonUtil.getInstance().fromJson(naviInfoJson, NaviInfoModel.class);
            if (naviModelList == null){
                return null;
            } else {
                return naviModelList;
            }
        }
    }

    public static String setUserNaviModelList(NaviInfoModel model){
        String naviInfoJson = GsonUtil.getInstance().toJson(model);
        SharePreUtil.SetShareString(NAVI_MODEL_LIST, naviInfoJson);
        return naviInfoJson;
    }

    public static User getUserInfoFromSP(){
        String userInfoJson = SharePreUtil.GetShareString(USER_NET);
        if (userInfoJson.length() <= 0){
            return null;
        } else {
            User user = GsonUtil.getInstance().fromJson(userInfoJson, User.class);
            if (user == null){
                return null;
            } else {
                return user;
            }
        }
    }

    public static String setUserInfoFromSP(User user){
        String userInfoJson = GsonUtil.getInstance().toJson(user);
        SharePreUtil.SetShareString(USER_NET, userInfoJson);
        return userInfoJson;
    }

    public static String setUserInfoLocalBySP(User user){
        String userInfoJson = GsonUtil.getInstance().toJson(user);
        SharePreUtil.SetShareString(USER_LOCAL, userInfoJson);
        return userInfoJson;
    }

    public static User getUserInfoLocalBySP(){
        String userInfoJson = SharePreUtil.GetShareString(USER_LOCAL);
        if (userInfoJson.length() <= 0){
            return null;
        } else {
            User user = GsonUtil.getInstance().fromJson(userInfoJson, User.class);
            if (user == null){
                return null;
            } else {
                return user;
            }
        }
    }


    public static void exitAccount(){
        SharePreUtil.SetShareString(USER_LOCAL, "");
        SharePreUtil.SetShareString(USER_NET, "");
    }

    public static void saveAndUpdateUserHeadImg(Uri imageUri){
        User user = null;
        try {
            //更新数据
            user = getUserInfoLocalBySP();
            Log.d("currentPath", imageUri+"");
            user.setHeadImg(imageUri+"");
            UserUtil.setUserInfoLocalBySP(user);
            User userInfoGson = getUserInfoFromSP();
            userInfoGson.setHeadImg("image/user/" + StringUtil.getFileName(user.getHeadImg()));
            String userJson = UserUtil.setUserInfoFromSP(userInfoGson);
            HttpManager.updateUserInfo(userJson);
            HttpManager.uploadUserInfo(user.getHeadImg());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
